package com.example.f_code;

import static android.content.ContentValues.TAG;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.f_code.db.bean.UserDatabase;
import com.example.f_code.db.bean.UserInfo;

public class RegisterActivity extends AppCompatActivity {
    EditText ed_user, ed_password, ed_password2;
    Button btn_register;
    UserDatabase userDB;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);

        userDB = MyApplication.getInstance().getUserDatabase();

        ed_user = findViewById(R.id.et_user);
        ed_password = findViewById(R.id.et_password);
        ed_password2 = findViewById(R.id.et_password2);

        btn_register = findViewById(R.id.btn_register);
        btn_register.setOnClickListener(v -> {
            register();
        });


        ImageView ic_back = findViewById(R.id.iv_back);
        ic_back.setOnClickListener(v -> {
            finish();
        });
    }

    private void register() {
        String user = ed_user.getText().toString();
        String password = ed_password.getText().toString();
        String password2 = ed_password2.getText().toString();
        if (user.isEmpty() || password.isEmpty() || password2.isEmpty()) {
            Toast.makeText(RegisterActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
        } else if (!password.equals(password2)) {
            Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
        } else {
            UserInfo userInfo = new UserInfo(user, password);
            userDB.userDao().insertUser(userInfo);
            // 注册成功
            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}